﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Windows;
using System.Windows.Markup;

namespace PrintManager
{
    public class PaperDefinition
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public double X0 { get; set; }
        public double Y0 { get; set; }
        public List<PaperElement> Elements { get; set; }

        FrameworkElement _layout;
        public FrameworkElement Layout 
        {
            get
            {
                if (_layout == null)
                    CreateLayout();
                return _layout;
            }
        }

        public static PaperDefinition Load(string filename)
        {
            CultureInfo culture = new CultureInfo("en-US");

            PaperDefinition pd = new PaperDefinition();
            if (File.Exists(filename))
            {
                pd.Elements = new List<PaperElement>();
                StreamReader sr = new StreamReader(filename, Encoding.Default);
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] ff = line.Split('=');
                    switch (ff[0].ToUpper())
                    {
                        case "ID":
                            pd.Id = ff[1];
                            break;
                        case "NAME":
                            pd.Name = ff[1];
                            break;
                        case "WIDTH":
                            pd.Width = double.Parse(ff[1], culture.NumberFormat);
                            break;
                        case "HEIGHT":
                            pd.Height = double.Parse(ff[1], culture.NumberFormat);
                            break;
                        case "X0":
                            pd.X0 = double.Parse(ff[1], culture.NumberFormat);
                            break;
                        case "Y0":
                            pd.Y0 = double.Parse(ff[1], culture.NumberFormat);
                            break;
                        case "ELEMENT":
                            string[] pe = ff[1].Split(',');
                            PaperElement e = new PaperElement
                            {
                                ID = pe[0],
                                X = double.Parse(pe[1], culture.NumberFormat),
                                Y = double.Parse(pe[2], culture.NumberFormat),
                                Width = double.Parse(pe[3], culture.NumberFormat),
                                Height = double.Parse(pe[4], culture.NumberFormat)
                            };
                            pd.Elements.Add(e);
                            break;
                    }

                }
                sr.Close();               
            }
            return pd;
        }

        void CreateLayout()
        {
            CultureInfo culture = new CultureInfo("en-US");

            StringBuilder xaml = new StringBuilder();
            xaml.AppendLine("<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'");
            xaml.AppendLine("xmlns:b='clr-namespace:Barcode;assembly=Barcode'");
            xaml.AppendLine("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");

            xaml.AppendLine(string.Format(culture.NumberFormat,
                "<Border BorderBrush='Black' BorderThickness='0.05' Width='{0}' Height='{1}' >", Width, Height));
            xaml.AppendLine("<Grid>");
            foreach (PaperElement e in Elements)
            {
                xaml.AppendLine(string.Format(culture.NumberFormat, "<Rectangle HorizontalAlignment='Left' VerticalAlignment='Top' " +
                    "Margin='{0},{1},0,0' Width='{2}' Height='{3}' Stroke='Black' StrokeThickness='0.05' />",
                    e.X, e.Y, e.Width, e.Height));
            }
            xaml.AppendLine("</Grid>");
            xaml.AppendLine("</Border>");
            xaml.AppendLine("</Grid>");
            string t = xaml.ToString();
            _layout = (FrameworkElement)XamlReader.Parse(t);
        }
    }
}
